package io.polly.ldz.reactor.mvc;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.BulkRequest;
import co.elastic.clients.elasticsearch.core.BulkResponse;
import co.elastic.clients.elasticsearch.core.CreateResponse;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author ldz112@163.com
 * @Classname QuestionController
 * @Version V1.0
 * @Description 实体操作类
 * @Date 2024/10/8 16:31
 * Copyright (C) 2023 All Rights Reserved
 */
@RestController
@Slf4j
public class QuestionController {

    final static String INDEX_NAME = "question";

    @Autowired
    private ElasticsearchClient elasticsearchClient;


    @RequestMapping("/")
    public String helloWorld(){
        String name = "李";
        log.info("hello{}",name);
        return "Hi,"+name;
    }

    @RequestMapping("/add")
    public void add(){

        Question question = new Question();
        question.setQuestionId(1L);
        question.setTitle("题目");
        question.setContent("题目内容");
        question.setBalance(20);
        question.setCreateTime(new Date());
        List<String> tags = Lists.newArrayList("GapFilling", "SingleChoice");
        question.setTags(tags);

        try {
            CreateResponse createResponse = elasticsearchClient.create(c -> c
                    .index(INDEX_NAME) // 索引名字
                    .id(String.valueOf(question.getQuestionId())) // id
                    .document(question) // 实体类
                    .routing(String.valueOf(question.getBalance()))
            );
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @RequestMapping("/batch")
    public void batch(){

        Question question = new Question();
        question.setQuestionId(2L);
        question.setTitle("题目2");
        question.setContent("题目内容2");
        question.setBalance(30);
        question.setCreateTime(new Date());

        List<String> tags = Lists.newArrayList("TrueFalse", "SingleChoice");
        question.setTags(tags);

        Question question2 = new Question();
        question2.setQuestionId(3L);
        question2.setTitle("题目bak");
        question2.setContent("题目内容bak");
        question2.setBalance(30);
        question2.setCreateTime(new Date());

        List<String> tags2 = Lists.newArrayList("TrueFalse", "SingleChoice");
        question2.setTags(tags2);

        List<Question> list= new ArrayList<>();
        list.add(question);
        list.add(question2);

        try {
            BulkRequest.Builder br = new BulkRequest.Builder();
            for (Question q : list) {
                br.operations(op -> op
                        .create(c -> c
                                .index(INDEX_NAME)
                                .id(String.valueOf(q.getQuestionId()))
                                .document(q)
                                .routing(String.valueOf(q.getBalance()))
                        )
                );
            }
            BulkResponse bulkResp = elasticsearchClient.bulk(br.build());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
